package com.night.icm.controller.base;

import com.night.icm.common.night_enum.CourseBaseType;
import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.SimpleDataDTO;
import com.night.icm.model.base.CourseTypeDTO;
import com.night.icm.service.base.service.CourseTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * CourseTypeController
 *
 * @author Liao ZhiYong
 * @date 2020/2/4
 */
@Api(tags = {"课程类型管理"})
@RestController
@RequestMapping("/api/icm-course-type")
public class CourseTypeController {

    private final CourseTypeService courseTypeService;

    @Autowired
    public CourseTypeController(CourseTypeService courseTypeService) {
        this.courseTypeService = courseTypeService;
    }


    @ApiOperation(value = "分页查询课程类型")
    @GetMapping(value = "")
    public PageDataDTO<CourseTypeDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "课程详细类型编号") @RequestParam(value = "code", required = false) String code,
            @ApiParam(value = "课程详细类型名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "课程基础类型") @RequestParam(value = "courseBaseType", required = false) CourseBaseType courseBaseType
    ) {
        return this.courseTypeService.findOnePage(page, size, sort, code, name, courseBaseType);
    }

    @ApiOperation(value = "新增课程类型")
    @PostMapping(value = "")
    public CourseTypeDTO create(
            @ApiParam(value = "课程类型信息", required = true) @RequestBody @Validated CourseTypeDTO courseTypeDTO
    ) {
        return this.courseTypeService.create(courseTypeDTO);
    }

    @ApiOperation(value = "删除课程类型")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "课程类型id", required = true) @PathVariable(name = "id") String id) {
        this.courseTypeService.delete(id);
    }

    @ApiOperation(value = "更新课程类型信息")
    @PutMapping(value = "/{id}")
    public CourseTypeDTO update(
            @ApiParam(value = "课程类型id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "课程类型信息", required = true) @RequestBody @Validated CourseTypeDTO courseTypeDTO
    ) {
        return this.courseTypeService.update(id, courseTypeDTO);
    }

    @ApiOperation(value = "根据id查找课程类型信息")
    @GetMapping(value = "/{id}")
    public CourseTypeDTO findById(@ApiParam(value = "课程类型id", required = true) @PathVariable(name = "id") String id) {
        return this.courseTypeService.findById(id);
    }

    @ApiOperation(value = "查找所有课程类型信息，可选择排序")
    @GetMapping(value = "/all")
    public List<CourseTypeDTO> findAll(
            @ApiParam(value = "排序参数") @RequestParam(value = "param", required = false, defaultValue = "id") String param,
            @ApiParam(value = "排序模式: asc/desc") @RequestParam(value = "pattern", required = false, defaultValue = "asc") String pattern
    ) {
        return this.courseTypeService.findAll(param, pattern);
    }

    @ApiOperation(value = "检查课程详细类型编号是否已存在")
    @GetMapping(value = "/checkCodeExist")
    public SimpleDataDTO<Boolean> checkCodeExist(
            @ApiParam(value = "课程详细类型编号", required = true) @RequestParam(value = "code") String code
    ) {
        return this.courseTypeService.checkCodeExist(code);
    }
}